package repository

import (
	model "antiquity_map/internal/models"
	"time"

	"gorm.io/gorm"
)

type CreateTime struct {
	EndTime   string `json:"endTime"`
	StartTime string `json:"startTime"`
}

type Location struct {
	Attitude float64 `json:"attitude"`
	Latitude float64 `json:"latitude"`
}

type UpdateTime struct {
	EndTime   string `json:"endTime"`
	StartTime string `json:"startTime"`
}
type Request struct {
	CreateTime *CreateTime `json:"createTime,omitempty"`
	Location   *Location   `json:"location,omitempty"`
	Name       *string     `json:"name,omitempty"`
	UpdateTime *UpdateTime `json:"updateTime,omitempty"`
}
type LocationInfo struct {
	CreaterCredit float64 `json:"createrCredit"`
	CreaterID     string  `json:"createrID"`
	Latitude      float64 `json:"latitude"`
	LocationID    float64 `json:"locationID"`
	LocationIntro string  `json:"locationIntro"`
	LocationName  string  `json:"locationName"`
	Longitude     float64 `json:"longitude"`
}

type Record struct {
	CreaterCredit float64 `json:"createrCredit"`
	CreaterID     string  `json:"createrID"`
	RecordID      string  `json:"recordID"`
	Thumbnail     string  `json:"thumbnail"`
	UploadTime    string  `json:"uploadTime"`
}
type Result struct {
	LocationInfo []LocationInfo `json:"locationInfo"`
	Records      []Record       `json:"records,omitempty"`
}

type RequestBody struct {
	Name         string `json:"name" binding:"required"`
	Introduction string `json:"introduction" binding:"required"`
	Location     struct {
		Longitude float64 `json:"longitude" binding:"required"`
		Latitude  float64 `json:"latitude" binding:"required"`
	} `json:"location" binding:"required"`
}

type LocationRepository interface {
	CreateLocation(Name string, Introduction string, Latitude float64, Longitude float64) error
	Filter(page string, pageSize string, onlyBriefInfo string, request Request, result *Result) error
	DeleteLocation(locationID string) error
	GetLocationDetail(locationID string) (Request_LocationDetail, error)
}

type Request_LocationDetail struct {
	LocationInfo []model.Location         `json:"locationInfo"`
	Records      []model.CollectionRecord `json:"records,omitempty"`
}

type locationRepository struct {
	db *gorm.DB
}

func NewLocationRepository(db *gorm.DB) LocationRepository {
	return &locationRepository{db: db}
}
func (r *locationRepository) CreateLocation(Name string, Introduction string, Latitude float64, Longitude float64) error {
	// Implement create location logic here
	var existingLocation model.Location
	if err := r.db.Where("longitude = ? AND latitude = ?", Longitude, Latitude).First(&existingLocation).Error; err == nil {
		return err
	}

	// 创建新地点
	newLocation := model.Location{
		Longitude:   Longitude,
		Latitude:    Latitude,
		Name:        Name,
		Description: Introduction,
		Status:      1, // 默认状态为1
		CreatedAt:   time.Now(),
		UpdatedAt:   time.Now(),
	}

	if err := r.db.Create(&newLocation).Error; err != nil {
		return err
	}
	return nil
}

func (r *locationRepository) Filter(page string, pageSize string, onlyBriefInfo string, request Request, result *Result) error {
	// Implement get location detail logic here
	if request.Name != nil && *request.Name != "" {
		err := r.db.Table("location").Where("name = ?", request.Name).Find(&result.LocationInfo).Error
		if err != nil {
			return err
		}
	}
	if(request.Location != nil){
		err := r.db.Table("location").Where("latitude = ? AND longitude = ?", request.Location.Latitude, request.Location.Attitude).Find(&result.LocationInfo).Error
		if err != nil {
			return err
		}
	}
	if request.CreateTime != nil {
		err := r.db.Table("location").Where("created_at BETWEEN ? AND ?", request.CreateTime.StartTime, request.CreateTime.EndTime).Find(&result.LocationInfo).Error
		if err != nil {
			return err
		}
	}
	if request.UpdateTime != nil {
		err := r.db.Table("location").Where("updated_at BETWEEN ? AND ?", request.UpdateTime.StartTime, request.UpdateTime.EndTime).Find(&result.LocationInfo).Error
		if err != nil {
			return err
		}
	}
	
	return nil
}

func (r *locationRepository) DeleteLocation(locationID string) error {
	// Implement delete location logic here
	result := r.db.Where("location_id = ?", locationID).Delete(&model.Location{})
	if result.Error != nil {
		return result.Error
	}

	if result.RowsAffected == 0 {
		return gorm.ErrRecordNotFound
	}
	return nil
}

func (r *locationRepository) GetLocationDetail(locationID string) (Request_LocationDetail, error) {
	var result Request_LocationDetail
	result.LocationInfo = []model.Location{}
	result.Records = []model.CollectionRecord{}
	// 查询地点信息
	var location_info []model.Location
	result1 := r.db.Where("location_id = ?", locationID).First(&location_info)

	if result1.Error != nil {
		return result, result1.Error
	}
	result.LocationInfo = location_info
	// 查询采集记录
	var records []model.CollectionRecord
	result2 := r.db.Where("location_id = ?", locationID).Find(&records)
	if result2.Error != nil {
		return result, result2.Error
	}
	result.Records = records
	return result, nil
}
